package TangkGame01;

import java.util.Vector;

public class EnyTank extends Tank implements Runnable{
    Vector<Shot> shots = new Vector<>();

    public EnyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true){
            if (isLive && shots.size() < 1) {
                Shot s = null;
                switch (direct) {
                    case 0:
                        s = new Shot(x + 20, y, 0);
                        break;
                    case 1:
                        s = new Shot(x + 60, y + 20, 1);
                        break;
                    case 2: //向下
                        s = new Shot(x + 20, y + 60, 2);
                        break;
                    case 3://向左
                        s = new Shot(x, y+ 20, 3);
                        break;
                }
                shots.add(s);
                //启动
                new Thread(s).start();
            }

            switch (direct){
                case 0:  //向上
                    //让坦克保持一个方向，走30步
                    for (int i = 0; i < 30; i++) {
                        if (y > 0) {
                            moveUp();
                        }
                        //休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:  //向右
                    for (int i = 0; i < 30; i++) {
                        if (x + 60 < 1000) {
                            moveRight();
                        }
                        //休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:  //向下
                    for (int i = 0; i < 30; i++) {
                        if (y + 60 < 750) {
                            moveDown();
                        }
                        //休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:  //向左
                    for (int i = 0; i < 30; i++) {
                        if (x > 0) {
                            moveLeft();
                        }
                        //休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            direct = (int) (Math.random() * 4);
            if (!isLive){
                break;
            }
        }
    }
}

